<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Cate;

class CateController extends Controller
{
    // 商品一级分类列表
    public function home_list(Request $request){
        $cates = Cate::where('pid',0)->orderBy('ord')->get();
        return response()->json(['msg'=>'获取成功!','status'=>'200','data'=>$cates]);
    }

     //商品二级分类列表
     public function sub_list(Request $request){
        $cates = Cate::where('pid',$request->pid)->orderBy('ord')->get();
        $cates = $this->get_imgs($cates);
        return response()->json(['msg'=>'获取成功!','status'=>'200','data'=>$cates]);
    }

    // 携带图片
    private function get_imgs($cates){
        foreach($cates as $k=>$cate){
            if(strlen($cate->img)>6){
                $cates[$k]['img'] = config('app.upload').$cate->img;
            }else{
                $cates[$k]['img'] = null;
            }
        }
        return $cates;
    }

    
}
